package it.azienda.stampe.controllo;

import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import it.azienda.framework.utilita.Utilita;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

public class CompilatoreModelliPDF {
    
    public CompilatoreModelliPDF(HashMap valori,int tipoStampa){
        if(tipoStampa == 1) {
            this.valori = valori;
            stampaFatturaImmediata();
        } else if(tipoStampa == 2) {
            this.valori = valori;
            stampaDdt();
        }
    }
    
    public CompilatoreModelliPDF(String nomeStampa){
        this.apriPdf(nomeStampa);
    }
    
    public static String pathModelliPDF = System.getenv("Utils_ManagerWork")+"\\stampePDF";
    private HashMap valori;
    private PdfWriter writer;
    private PdfReader reader2;
    
    public void stampaFatturaImmediata(){
        try {
            
            BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            // we create a reader for a certain document
            reader2 = new PdfReader(pathModelliPDF+"\\Fattura.pdf");
            int num=0;
            // we retrieve the total number of pages
            int n = reader2.getNumberOfPages();
            // we retrieve the size of the first page
            Rectangle psize = reader2.getPageSize(1);
            float width = psize.height();
            float height = psize.width();
            
            // step 1: creation of a document-object
            Document document = new Document();
            // step 2: we create a writer that listens to the document
            //Salvare su disco-->  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\FileGenerato"+num+".pdf"));
            ByteArrayOutputStream out=new ByteArrayOutputStream();
            writer = PdfWriter.getInstance(document, out);
            // step 3: we open the document
            document.open();
            int page=1;
            // step 4: we add content
            PdfContentByte cb = writer.getDirectContent();
            
            String prodotti= (String) this.valori.get("0");
            Integer numProdotti = new Integer(prodotti);
            
            int numPagina = 1;
            int numPagine = 0;
            numPagine = numProdotti.intValue() / 36;
            if(numProdotti.intValue() % 36 > 0){
                numPagine += 1;
            }
            
            document.newPage();
            
            PdfImportedPage page1 = writer.getImportedPage(reader2, 1);
            cb.addTemplate(page1, 1, 0, 0, 1, 0, 0);
            cb.beginText();
            cb.setFontAndSize(bf, 10);
            
            riempiPdf(parametriFatturaImmediata(),cb,bf, this.valori );
            
            ArrayList listaPagine = (ArrayList) this.valori.get("pagine");
            int pp = 1;
            for(int p=0; p<listaPagine.size(); p++){
                if(p != 0){
                    cb.endText();
                    document.newPage();
                    page1 = writer.getImportedPage(reader2, 1);
                    cb.addTemplate(page1, 1, 0, 0, 1, 0, 0);
                    cb.beginText();
                    cb.setFontAndSize(bf, 10);
                }
                HashMap mappaProdotti = (HashMap)listaPagine.get(p);
                mappaProdotti.put("18",pp+"/"+numPagine);
                riempiPdf(parametriFatturaImmediata(),cb,bf, mappaProdotti);
                pp++;
            }
            
            // cb.setTextMatrix(30, 30);
            // cb.showText("Pag. "+page++);
            cb.endText();
            
            // step 5: we close the document
            document.close();
            
            byte[] bytes =out.toByteArray();
            Utilita.getRequest().setAttribute("byte",bytes);
            String nomeFile="Fattura.pdf";
            Utilita.getRequest().setAttribute("file",nomeFile);
            String url2 = "/pages_stampe/visualizzaPdf.jsf";
            FacesContext ctx = FacesContext.getCurrentInstance();
            UIViewRoot view = ctx.getApplication().getViewHandler().createView(ctx,url2);
            view.setViewId(url2);
            ctx.setViewRoot(view);
            ctx.renderResponse();
            
        } catch (Exception de) {
            de.printStackTrace();
        }
    }
    
    public void stampaDdt() {
        try {
            BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            // we create a reader for a certain document
            reader2 = new PdfReader(pathModelliPDF+"\\Ddt.pdf");
            int num=0;
            // we retrieve the total number of pages
            int n = reader2.getNumberOfPages();
            // we retrieve the size of the first page
            Rectangle psize = reader2.getPageSize(1);
            float width = psize.height();
            float height = psize.width();
            
            // step 1: creation of a document-object
            Document document = new Document();
            // step 2: we create a writer that listens to the document
            //Salvare su disco-->  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\FileGenerato"+num+".pdf"));
            ByteArrayOutputStream out=new ByteArrayOutputStream();
            writer = PdfWriter.getInstance(document, out);
            // step 3: we open the document
            document.open();
            int page=1;
            // step 4: we add content
            PdfContentByte cb = writer.getDirectContent();
            
            String prodotti= (String) this.valori.get("0");
            Integer numProdotti = new Integer(prodotti);
            
            int numPagina = 1;
            int numPagine = 0;
            numPagine = numProdotti.intValue() / 46;
            if(numProdotti.intValue() % 46 > 0){
                numPagine += 1;
            }
            
            document.newPage();
            
            PdfImportedPage page1 = writer.getImportedPage(reader2, 1);
            cb.addTemplate(page1, 1, 0, 0, 1, 0, 0);
            cb.beginText();
            cb.setFontAndSize(bf, 10);
            
            riempiPdf(parametriDdt(),cb,bf, this.valori);
            
            ArrayList listaPagine = (ArrayList) this.valori.get("pagine");
            int pp = 1;
            for(int p=0; p<listaPagine.size(); p++){
                if(p != 0){
                    cb.endText();
                    document.newPage();
                    page1 = writer.getImportedPage(reader2, 1);
                    cb.addTemplate(page1, 1, 0, 0, 1, 0, 0);
                    cb.beginText();
                    cb.setFontAndSize(bf, 10);
                }
                HashMap mappaProdotti = (HashMap)listaPagine.get(p);
                mappaProdotti.put("18",pp+"/"+numPagine);
                riempiPdf(parametriDdt(),cb,bf, mappaProdotti);
                pp++;
            }
            
            // cb.setTextMatrix(30, 30);
            // cb.showText("Pag. "+page++);
            cb.endText();
            
            // step 5: we close the document
            document.close();
            
            byte[] bytes =out.toByteArray();
            Utilita.getRequest().setAttribute("byte",bytes);
            String nomeFile="ddt.pdf";
            Utilita.getRequest().setAttribute("file",nomeFile);
            String url2 = "/pages_stampe/visualizzaPdf.jsf";
            FacesContext ctx = FacesContext.getCurrentInstance();
            UIViewRoot view = ctx.getApplication().getViewHandler().createView(ctx,url2);
            view.setViewId(url2);
            ctx.setViewRoot(view);
            ctx.renderResponse();
            
        } catch (Exception de) {
            de.printStackTrace();
        }
    }
    
    public HashMap parametriFatturaImmediata(){
        
        HashMap parametri=new HashMap();
        parametri.put("1","173-32-0-font13-20");
        parametri.put("2","173-54-0-font11-24");
        // parametri.put("3","173-77-0-font9-30");//Indirizzo
        // parametri.put("4","173-99-0-font9-30");//Comune
        // parametri.put("3","20-100-0-font9-28");//Indirizzo
        // parametri.put("4","173-100-0-font9-30");//Comune
        // parametri.put("5","20-120-0-font9-58");
        // parametri.put("6","20-140-0-font9-58");
        // parametri.put("7","20-160-0-font9-58");
        parametri.put("8","310-44-0-font10-45");
        parametri.put("9","310-59-0-font10-45");
        parametri.put("10","310-74-0-font10-45");
        parametri.put("11","310-89-0-font10-45");
        parametri.put("11b","310-104-0-font10-45");
        parametri.put("12","310-130-0-font10-45");
        parametri.put("13","310-150-0-font10-45");
        parametri.put("14","16-191-0- -25");
        parametri.put("15a","214-180-0- -25");
        parametri.put("15b","214-192-0- -25");
        parametri.put("16","377-191-2- -10");
        parametri.put("17","386-191-0-dd mm aaaa-10");
        
        parametri.put("18","490-191-2- -5");
        parametri.put("19","577-191-2- -10");
        // parametri.put("20","500-191-0- -15");
        
        parametri.put("21","16-215-0- -34");
        parametri.put("22","234-215-0- -57");
        
        String numProdotti= (String) this.valori.get("0");
        Integer numP=new Integer(numProdotti);
        
        numProdotti= (String) this.valori.get("0");
        int yy=250;
        for(int y=0;y<new Integer(numProdotti).intValue();y++){
            
            if(y % 36 == 0){
                yy = 250;
            }
            
            int codice = yy-1;
            parametri.put("23"+y,"71-"+codice +"-2-font6-16");
            parametri.put("24a"+y,"76-"+yy +"-0-font8-51");
            parametri.put("25"+y,"339-"+yy +"-0-font8-4");
            parametri.put("26"+y,"397-"+yy +"-2-font8-6");
            parametri.put("27"+y,"460-"+yy +"-2-font8-10");
            parametri.put("28"+y,"492-"+yy +"-2-font8-5");
            parametri.put("29"+y,"554-"+yy +"-2-font8-12");
            parametri.put("30"+y,"582-"+yy +"-2-font8-5");
            //yy+=10;
            //parametri.put("24b"+y,"69-"+yy +"-0-font8-49");
            yy+=10;
        }
        //24
        parametri.put("31","106-647-2- -12");
        parametri.put("32","163-647-2- -12");
        parametri.put("33","266-647-2- -12");
        parametri.put("34","369-647-2- -12");
        parametri.put("35","580-656-2- -12");
        
        parametri.put("36a","15-667-0-font8-4");
        parametri.put("36b","192-667-2-font8-12");
        parametri.put("36c","237-667-2-font8-5");
        parametri.put("36d","367-667-2-font8-12");
        
        parametri.put("37a","15-676-0-font8-4");
        parametri.put("37b","192-676-2-font8-12");
        parametri.put("37c","237-676-2-font8-5");
        parametri.put("37d","367-676-2-font8-12");
        
        parametri.put("38a","15-685-0-font8-4");
        parametri.put("38b","192-685-2-font8-12");
        parametri.put("38c","237-685-2-font8-5");
        parametri.put("38d","367-685-2-font8-12");
        
        parametri.put("21a","15-694-0-font8-4");
        parametri.put("21b","192-694-2-font8-12");
        parametri.put("21c","237-694-2-font8-5");
        parametri.put("21d","367-694-2-font8-12");
        
        parametri.put("39","579-692-2- -12");
        
        parametri.put("40","16-708-0- -30");
        parametri.put("40a","16-724-0-dd mm aaaa-10");
        parametri.put("40b","100-724-0-dd mm aaaa-10");
        parametri.put("40c","185-724-0-dd mm aaaa-10");
        parametri.put("40d","269-724-0-dd mm aaaa-10");
        
        parametri.put("41a","16-739-0-font9-10");
        parametri.put("41b","100-739-0-font9-10");
        parametri.put("41c","185-739-0-font9-10");
        parametri.put("41d","269-739-0-font9-10");
        
        parametri.put("42","579-737-2-font12-12");
        parametri.put("43","15-767-0- -38");
        parametri.put("44","264-767-0- -22");
        parametri.put("45","15-795-0- -17");
        parametri.put("46","125-795-0- -22");
        parametri.put("47","263-795-0- -23");
        parametri.put("47a","359-715-0-dd mm aaaa-10");
        parametri.put("47b","359-739-0- -5");
        
        parametri.put("48","377-643-0- -5");
        parametri.put("49","377-666-0- -5");
        parametri.put("50","377-690-0- -12");
        
        parametri.put("51","15-821-0- -64");
        return parametri;
    }
    
    public HashMap parametriDdt() {
        HashMap parametri = new HashMap();
        parametri.put("1", "173-32-0-font13-20");
        parametri.put("2", "173-54-0-font11-24");
        parametri.put("8", "310-44-0-font10-45");
        parametri.put("9", "310-59-0-font10-45");
        parametri.put("10", "310-74-0-font10-45");
        parametri.put("11", "310-89-0-font10-45");
        parametri.put("11b", "310-104-0-font10-45");
        parametri.put("12", "310-130-0-font10-45");
        parametri.put("13", "310-150-0-font10-45");
        parametri.put("14", "16-202-0- -25");
        parametri.put("15a","227-190-0- -25");
        parametri.put("15b","227-202-0- -25");
        parametri.put("16", "418-202-2- -10");
        parametri.put("17", "438-202-0-dd mm aaaa-10");
        parametri.put("18", "580-202-2- -5");
        String numProdotti = (String)valori.get("0");
        Integer numP = new Integer(numProdotti);
        numProdotti = (String)valori.get("0");
        int yy = 250;
        for(int y = 0; y < (new Integer(numProdotti)).intValue(); y++) {
            
            if(y % 46 == 0){
                yy = 250;
            }
            
            parametri.put("19" + y, "72-" + yy + "-2-font6-16");
            parametri.put("20a" + y, "76-" + yy + "-0-font8-51");
            parametri.put("21" + y, "339-" + yy + "-0-font8-4");
            parametri.put("22" + y, "397-" + yy + "-2-font8-6");
            parametri.put("23" + y, "460-" + yy + "-2-font8-10");
            parametri.put("24" + y, "492-" + yy + "-2-font8-5");
            parametri.put("25" + y, "554-" + yy + "-2-font8-12");
            parametri.put("26" + y, "585-" + yy + "-2-font8-5");
            yy += 10;
        }
        
        parametri.put("27", "15-739-0- -15");
        parametri.put("28", "127-739-0- -10");
        parametri.put("29", "190-739-0- -10");
        parametri.put("30", "263-739-0- -21");
        
        parametri.put("31", "15-767-0- -30");
        parametri.put("32", "263-767-0- -21");
        parametri.put("33", "15-795-0- -15");
        parametri.put("34", "127-795-0- -15");
        parametri.put("34a", "263-795-0-dd mm aaaa-10");
        parametri.put("34b", "335-795-0- -5");
        parametri.put("35", "15-821-0- -64");
        return parametri;
    }
    
    public void riempiPdf(HashMap parametri,PdfContentByte cb,BaseFont bf, HashMap valoriStampa){
        ArrayList listaChiavi=new ArrayList(parametri.keySet());
        
        try {
            for(int f=0;f<listaChiavi.size();f++){
                String chiave=(String)listaChiavi.get(f);
                String[] valore= parametri.get(chiave).toString().split("-");
                Integer xx=new Integer(valore[0]);
                Integer yy=new Integer(valore[1]);
                Integer align=new Integer(valore[2]);
                String tipo=valore[3].toString();
                Integer lunghezza=new Integer(valore[4]);
                yy=new Integer(842-yy.intValue());
                if(valoriStampa!=null){
                    String prop="";
                    try{
                        prop= valoriStampa.get(chiave).toString();
                    }catch(Exception exx){
                        
                    }
                    if(prop.length()>lunghezza.intValue()){
                        prop=prop.substring(0,lunghezza.intValue());
                    }
                    if(tipo.equals("split")){
                        int x=0;
                        int y=1;
                        String propApp="";
                        for(int g=0;g<prop.length();g++){
                            propApp+=prop.substring(x++,y++)+" ";
                        }
                        cb.setFontAndSize(bf, 12);
                        prop=propApp;
                    }else if(tipo.equals("dd mm aaaa")){
                        String[] propApp=prop.split("-");
                        try{
                            prop=propApp[2].toString()+"/"+propApp[1].toString()+"/"+propApp[0].toString();
                        }catch(Exception ex){}
                    }else if(tipo.contains("font")){
                        Integer font=new Integer(tipo.substring(4,tipo.length()));
                        cb.setFontAndSize(bf, font.floatValue());
                    }
                    try{
                        if(new Double(prop) instanceof Double){
                            if(prop.contains(".")){
                                String pp=prop.substring(prop.indexOf(".")+1,prop.length());
                                if(pp.length()==1){
                                    prop=prop+"0";
                                    if(prop.length()>lunghezza.intValue()){
                                        prop=prop.substring(0,lunghezza.intValue());
                                    }
                                }
                            }
                        }
                    }catch(Exception e){
                    }
                    cb.showTextAligned(align.intValue(), prop, xx.floatValue(), yy.floatValue(), 0);
                    cb.setFontAndSize(bf, 10);
                }else{
                    cb.showTextAligned(align.intValue(), "", xx.floatValue(), yy.floatValue(), 0);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public HashMap getValori() {
        return valori;
    }
    
    public void setValori(HashMap valori) {
        this.valori = valori;
    }
    
    public void apriPdf(String nomeStampa){
        try {
            BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            // we create a reader for a certain document
            PdfReader reader2 = new PdfReader(pathModelliPDF+"\\"+nomeStampa+".pdf");
            int num=0;
            // we retrieve the total number of pages
            int n = reader2.getNumberOfPages();
            // we retrieve the size of the first page
            Rectangle psize = reader2.getPageSize(1);
            float width = psize.height();
            float height = psize.width();
            
            // step 1: creation of a document-object
            Document document = new Document();
            // step 2: we create a writer that listens to the document
            
            ByteArrayOutputStream out=new ByteArrayOutputStream();
            PdfWriter writer = PdfWriter.getInstance(document, out);
            
            int numPag = 1;
            // step 3: we open the document
            document.open();
            int page=1;
            // step 4: we add content
            PdfContentByte cb = writer.getDirectContent();
            int i = 0;
            
            document.newPage();
            i++;
            numPag--;
            PdfImportedPage page1 = writer.getImportedPage(reader2, 1);
            cb.addTemplate(page1, 1, 0, 0, 1, 0, 0);
            cb.beginText();
            cb.setFontAndSize(bf, 10);
            cb.endText();
            
            // step 5: we close the document
            document.close();
            
            byte[] bytes =out.toByteArray();
            Utilita.getRequest().setAttribute("byte",bytes);
            String nomeFile=nomeStampa+".pdf";
            Utilita.getRequest().setAttribute("file",nomeFile);
            String url2 = "/pages_stampe/visualizzaPdf.jsf";
            FacesContext ctx = FacesContext.getCurrentInstance();
            UIViewRoot view = ctx.getApplication().getViewHandler().createView(ctx,url2);
            view.setViewId(url2);
            ctx.setViewRoot(view);
            ctx.renderResponse();
            
        } catch (Exception de) {
            de.printStackTrace();
        }
    }
}
